package com.example.courseplus.Activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.courseplus.DAO.AdminDAO;
import com.example.courseplus.DAO.StudentDAO;
import com.example.courseplus.DAO.TeacherDAO;
import com.example.courseplus.R;

public class LoginActivity extends AppCompatActivity {
    Toolbar toolbar;
    Button login_button;
    Button register_button;
    Activity activity;
    String identity;
    RadioGroup radioGroup;
    EditText username;
    EditText password;
    RadioButton student;
    RadioButton teacher;
    RadioButton admin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        toolbar = findViewById(R.id.login_toolbar);
        login_button = findViewById(R.id.login);
        register_button = findViewById(R.id.register);
        radioGroup = findViewById(R.id.login_group);
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);
        student = findViewById(R.id.login_student);
        teacher = findViewById(R.id.login_teacher);
        admin = findViewById(R.id.login_admin);
        //根据选择的身份处理登陆的身份
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.login_student:
                        activity = new StudentActivity();
                        identity = "学生";
                        break;
                    case R.id.login_teacher:
                        activity = new TeacherActivity();
                        identity = "老师";
                        break;
                    case R.id.login_admin:
                        activity = new AdminActivity();
                        identity = "管理员";
                        break;
                    default:
                        break;
                }
            }
        });

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(activity!=null){
                    login(identity);
                }else {
                    Toast.makeText(getBaseContext(), "请选择你的身份", Toast.LENGTH_SHORT).show();
                }
            }
        });

        /**
         * 跳转到注册界面
         */
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    /**
     * 根据不同的身份查询数据库，并根据不同的情况给出不同的提示
     * @param identity
     */
    private void login(String identity) {
        String USERNAME = username.getText().toString();
        String PASSWORD = password.getText().toString();
        switch (identity){
            case "学生":
                StudentDAO studentDAO = new StudentDAO();
                if(studentDAO.checkLogin(USERNAME,PASSWORD)){
                    Intent intent = new Intent(LoginActivity.this, activity.getClass());
                    intent.putExtra("username",USERNAME);
                    startActivity(intent);
                    onDestroy();
                } else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误！", Toast.LENGTH_SHORT).show();
                }
                break;
            case "老师":
                TeacherDAO teacherDAO = new TeacherDAO();
                if(teacherDAO.checkLogin(USERNAME,PASSWORD)){
                    Intent intent = new Intent(LoginActivity.this, activity.getClass());
                    intent.putExtra("username",USERNAME);
                    startActivity(intent);
                    onDestroy();
                } else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误！", Toast.LENGTH_SHORT).show();
                }
                break;
            case "管理员":
                AdminDAO adminDAO = new AdminDAO();
                if(adminDAO.checkLogin(USERNAME,PASSWORD)){
                    Intent intent = new Intent(LoginActivity.this, activity.getClass());
                    intent.putExtra("username",USERNAME);
                    startActivity(intent);
                    onDestroy();
                } else {
                    Toast.makeText(LoginActivity.this,"账号或密码错误！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    /**
     * 注册返回的页面
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if (resultCode==RESULT_OK){
                    String returnUsername = data.getStringExtra("username");
                    String returnIdentity = data.getStringExtra("identity");
                    username.setText(returnUsername);
                    switch (returnIdentity){
                        case "学生":
                            student.setChecked(true);
                            break;
                        case "老师":
                            teacher.setChecked(true);
                            break;
                        case "管理员":
                            admin.setChecked(true);
                            break;
                        default:
                            break;
                    }
                }
                break;
            default:
                break;
        }
    }
}